package com.gzs.support.fasfdfs;

import java.io.Serializable;
import java.util.Collection;

import org.apache.log4j.Logger;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;


@Component
public class FastDFSManager implements Serializable  {
	private static Logger log = Logger.getLogger(FastDFSManager.class);
	
	private static final long serialVersionUID = 1L;
    private static TrackerClient trackerClient;
    private static TrackerServer trackerServer;
    private static StorageServer storageServer;
    private static StorageClient storageClient;
    
    static {
        try {
            trackerClient = new TrackerClient(FastDFSClientFactory.g_tracker_group);
            trackerServer = trackerClient.getConnection();
            storageClient = new StorageClient(trackerServer, storageServer);
        } catch (Exception e) {
        	log.info("FastDFSHandler   ----------    初始化异常");
            e.printStackTrace();
        }
    }

    /**
     * 文件上传
     * @param file
     * @param valuePairs
     * @return
     */
    public static String upload(String trackerNginxAddr,FastDFSFile fastDFSFile,NameValuePair[] valuePairs) {
        String[] uploadResults = null;
        try {
            uploadResults = storageClient.upload_file(fastDFSFile.getContent(),fastDFSFile.getExt(), valuePairs);
        } catch (Exception e) {
        	log.info("FastDFSHandler   ----------    文件上传异常");
            e.printStackTrace();
        }
        String groupName = uploadResults[0];
        String remoteFileName = uploadResults[1];
        String fileAbsolutePath = trackerNginxAddr
                //+ trackerServer.getInetSocketAddress().getHostName()
                //+ SEPARATOR + TRACKER_NGNIX_PORT 
                + "/" + groupName
                + "/" + remoteFileName;
        return fileAbsolutePath;
    }
    
    /**
     * 文件下载
     * @param groupName
     * @param remoteFileName
     * @param specFileName
     * @return
     */
    public static ResponseEntity<byte[]> download(String groupName,
            String remoteFileName,String specFileName) {
        byte[] content = null;
        HttpHeaders headers = new HttpHeaders();
        try {
            content = storageClient.download_file(groupName, remoteFileName);
            headers.setContentDispositionFormData("attachment",  new String(specFileName.getBytes("UTF-8"),"iso-8859-1"));
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        } catch (Exception e) {
        	log.info("FastDFSHandler   ----------    文件下载异常");
            e.printStackTrace();
        }
        return new ResponseEntity<byte[]>(content, headers, HttpStatus.CREATED);
    }
}

